package hot_100;

public class 二叉树的直径 {
    public static void main(String[] args) {

    }


    int ans = 0;
    public int diameterOfBinaryTree(TreeNode root) {

        Dfs(root);
        return ans;
    }
    private int Dfs(TreeNode root) {
        if (root == null)
            return 0;
        int lc = Dfs(root.left);
        int rc = Dfs(root.right);
        ans = Math.max(ans, lc + rc);
        return Math.max(lc, rc) + 1;
    }

    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }
}
